﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 链接分页列表类。
	/// </summary>
	public class LinkPageList : LinkList
	{
		#region
		/// <summary>
		/// 分页的查询字段。
		/// </summary>
		public string PageField { get { return GenericUrlManager.Link_Query_PageField; } }

		/// <summary>
		/// 分页的列表Url模式。
		/// </summary>
		public string ListUrlPattern { get { return GenericUrlManager.BuildLinkListUrlPattern(ClassCode); } }
		#endregion





		/// <summary>
		/// 分页控件。
		/// </summary>
		protected Wuqi.Webdiyer.AspNetPager CtrPager;


		private LoadedValue<string> loadedClassCode;
		/// <summary>
		/// 列表的模块代码。
		/// </summary>
		public override string ClassCode
		{
			get
			{
				if (loadedClassCode.Loader == null)
					loadedClassCode.Loader = () => Request.QueryString[GenericUrlManager.Link_Query_ClassCodeField];
				return loadedClassCode.Value;
			}
			set
			{
				loadedClassCode.Value = value;
				loadedLinkClassInfo.ResetValue();
			}
		}

		private LoadedValue<int?> loadedChannelId;
		public override int? ChannelId
		{
			get
			{
				if (loadedChannelId.Loader == null)
					loadedChannelId.Loader = () => IntExtensions.ParseOrNull(GenericUrlManager.Vote_Query_ChannelField);
				return loadedChannelId.Value;
			}
		}


		
		private LoadedValue<int> loadedCurrentPage;
		/// <summary>
		/// 列表的当前页码。
		/// </summary>
		public virtual int CurrentPage
		{
			get
			{
				if (loadedCurrentPage.Loader == null)
					loadedCurrentPage.Loader = () => IntExtensions.ParseOrDefault(Request.QueryString[PageField], 1);
				return loadedCurrentPage.Value;
			}
			set
			{
				loadedCurrentPage.Value = value;
			}
		}

		private LoadedValue<SearchType> loadedSearchType;
		/// <summary>
		/// 列表的检索类型。
		/// </summary>
		public virtual SearchType SearchType
		{
			get
			{
				if (loadedSearchType.Loader == null)
					loadedSearchType.Loader = () => EnumExtensions.ParseOrDefault<SearchType>(Request.QueryString["SearchType"], SearchType.None);
				return loadedSearchType.Value;
			}
		}

		private LoadedValue<int> loadedStoragePageSize;
		/// <summary>
		/// 列表的文章模块代码。
		/// </summary>
		public virtual int StoragePageSize
		{
			get
			{
				if (loadedStoragePageSize.Loader == null)
					loadedStoragePageSize.Loader = () => IntExtensions.ConvertFromOrDefault(ViewState["StoragePageSize"], 20);
				return loadedStoragePageSize.Value;
			}
			set
			{
				if (loadedStoragePageSize.Setter == null)
					loadedStoragePageSize.Setter = v => ViewState["StoragePageSize"] = v;
				loadedStoragePageSize.Value = value;
			}
		}

		/// <summary>
		/// 列表的页尺寸。
		/// </summary>
		public int PageSize
		{
			get
			{
				return CtrPager == null ? StoragePageSize : CtrPager.PageSize;
			}
			set
			{
				if (CtrPager != null)
					CtrPager.PageSize = value;
				StoragePageSize = value;
			}
		}

		public override string DateTimeField
		{
			get { return "CreationDate"; }
		}


		protected override void OnInit(EventArgs e)
		{
			if (CtrPager != null)
			{
				CtrPager.UrlPageIndexName = GenericUrlManager.Article_Query_PageField;
				if (SearchType != SearchType.None || string.IsNullOrEmpty(ListUrlPattern))
				{
					CtrPager.EnableUrlRewriting = false;
				}
				else
				{
					CtrPager.EnableUrlRewriting = true;
					CtrPager.UrlRewritePattern = ListUrlPattern;
				}
			}
			base.OnInit(e);
		}
		
		/// <summary>
		/// 获取列表的数据。
		/// </summary>
		/// <returns>列表的数据。</returns>
		public override object GetListData()
		{
			PaginationInfo<List<Links.LinkForImageList>> pageInfo = Links.GetPaginationImageList(SiteId, ClassCode, PageSize, CurrentPage);
			if (CtrPager != null)
			{
				CtrPager.RecordCount = pageInfo.RecordCount;
				CtrPager.CustomInfoHTML = GenericHelper.GetPagerCustomInfoHtml<List<Links.LinkForImageList>>(pageInfo);
			}
			return pageInfo.Records;
		}

		public virtual void RewriteMeta()
		{
			this.Page.Title = (LinkClassInfo == null ? Res.LinkList : LinkClassInfo.ClassName) +
				(CurrentPage > 1 ? Hxsoft.AppV1.Page.TitleSectionSeparator + string.Format(Res.TitlePaging, CurrentPage) : "")
				+ Hxsoft.AppV1.Page.TitleSuffix;
		}

		protected override void OnPreRender(EventArgs e)
		{
			RewriteMeta();

			base.OnPreRender(e);
		}
	}
}